package ute.mvc.controller.portal.student;

import java.util.ArrayList;
import java.util.List;

import javax.xml.ws.BindingType;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import ute.mvc.dto.StudentAssignmentDTO;
import ute.mvc.model.Assignment;
import ute.mvc.model.MyClass;
import ute.mvc.model.OpenCourse;
import ute.mvc.model.OpenCourse_Subject;
import ute.mvc.model.SubmittedAssignment;
import ute.mvc.model.TestAndQuizz;
import ute.mvc.model.User;
import ute.mvc.service.AssignmentService;
import ute.mvc.service.Course_SubjectService;
import ute.mvc.service.FileService;
import ute.mvc.service.OpenCourseService;
import ute.mvc.service.SubmittedAssignmentService;
import ute.mvc.service.UserService;
import ute.mvc.util.JsonObject;

@Controller
@RequestMapping(value = { "/student/{studentId}/{openCourseId}/{subjectId}/assignment" })
public class StudentAssignmentController {

	@Autowired
	Course_SubjectService course_SubjectService;
	@Autowired
	FileService fileService;
	@Autowired
	SessionFactory sessionFactory;
	@Autowired
	OpenCourseService openCourseService;
	@Autowired
	Course_SubjectService courseSubjectService;
	@Autowired
	UserService userService;
	@Autowired
	AssignmentService assignmentService;
	@Autowired
	SubmittedAssignmentService submittedAssignmentService;

	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String getSubjectAssignment(Model model) {

		System.out.println("list assignment");
		return "student_assignment_list";
	}

	@RequestMapping(value = "/{assignmentId}", method = RequestMethod.GET)
	public String getAssignment(@PathVariable int studentId,
			@PathVariable String openCourseId, @PathVariable String subjectId,
			@PathVariable String assignmentId, Model model) {

		Assignment assignment = assignmentService
				.getAssignmentById(assignmentId);

		model.addAttribute("assignment", assignment);
		model.addAttribute("file", assignment.getFile());

		StudentAssignmentDTO studentAssignmentDTO = assignmentService
				.getStudentAssignmentDTO(assignmentId, studentId);
		model.addAttribute("studentAssignmentDTO", studentAssignmentDTO);
		model.addAttribute("studentFile", studentAssignmentDTO.getmFile());
		System.out.println("submit allow:" + studentAssignmentDTO.isAllowSubmitt());
		
		return "student_assignment_detail";
	}

	@RequestMapping(value = "/{assignmentId}/submit", method = RequestMethod.POST)
	public String submitAssignment(
			@ModelAttribute("studentAssignmentDTO") StudentAssignmentDTO studentAssignmentDTO,
			BindingResult result, @PathVariable int studentId,
			@PathVariable String openCourseId, @PathVariable String subjectId,
			@PathVariable String assignmentId, Model model) {
		submittedAssignmentService.submit(assignmentId, studentId, studentAssignmentDTO);
		
		return "redirect:";
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@RequestMapping(value = "/listJSON")
	@ResponseBody
	public JsonObject getAssignmentList(@PathVariable int studentId,
			@PathVariable String openCourseId, @PathVariable String subjectId) {

		User student = userService.getUserById(studentId);
		OpenCourse oCourse = openCourseService.getOpenCourseById(openCourseId);
		MyClass uClass = openCourseService.getClass(student, oCourse);

		OpenCourse_Subject oCourseSubject = courseSubjectService
				.getCourse_Subject(openCourseId + "_" + subjectId);

		List<Assignment> lstAssignment = assignmentService
				.getAssignmentsForStudent(uClass, oCourseSubject);
		System.out.println(uClass.getClassName());
		System.out.println("List Test: " + lstAssignment.size());
		JsonObject jsonObject = new JsonObject();
		jsonObject.setAaData(lstAssignment);
		jsonObject.setsEcho(1);
		jsonObject.setiTotalDisplayRecords(lstAssignment.size());
		jsonObject.setiTotalRecords(lstAssignment.size());
		return jsonObject;
	}
}
